﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using PlanningGIS.ArcGIS;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Framework;
using PlanningGIS.Framework.Controls;
using PlanningGIS.Util;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.SelectQuery
{
    public partial class QueryByGeometry  : CtlDrawGeo, ICommand
    {
        /// <summary>
        /// 绘制完成事件
        /// </summary>
        public event DrawFinishedEventHandler DrawFinished;

        /// <summary>
        /// 查询窗体
        /// </summary>
        private FrmQuery m_frmQuery = null;

        public QueryByGeometry()
        {
            InitializeComponent();
        }

        public QueryByGeometry(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        private IApplication m_Application = null;


        protected override void ToolCreator_DrawFinished(object sender, DrawArgs e)
        {
            if (e.Geometry != null)
            {
                if (e.DrawElement != null)
                {
                    DrawElement = e.DrawElement;
                    IElementProperties ep = DrawElement as IElementProperties;
                    ep.Name = _Name + DateTime.Now.ToString("HHmmssttt");
                }

                if (DrawFinished != null) DrawFinished(this, e);

                if (e.Geometry == null || e.Geometry.IsEmpty) return;

                ISelectionEnvironment selectionEnv = new SelectionEnvironmentClass();
                double tol = selectionEnv.SearchTolerance;
                if (e.Shift == 1)
                {
                    selectionEnv.CombinationMethod = esriSelectionResultEnum.esriSelectionResultXOR;
                }
                else if (e.Shift == 2)
                {
                    selectionEnv.CombinationMethod = esriSelectionResultEnum.esriSelectionResultAdd;
                }
                else
                {
                    selectionEnv.CombinationMethod = esriSelectionResultEnum.esriSelectionResultNew;
                }
                selectionEnv.AreaSelectionMethod = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;
                IActiveView m_ActiveView = (m_Application.Document as IMxDocument).ActiveView;
                double dblBuffer = DisplayHelper.ConvertPixelsToMapUnits(m_ActiveView, tol);

                if (e.Geometry == null || e.Geometry.IsEmpty) return;
                IGeometry pPolygon = ((ITopologicalOperator)e.Geometry).Buffer(dblBuffer);


                m_frmQuery = FrmQuery.Instance();
                m_frmQuery.Application = m_Application;
                m_frmQuery.DefaultShowSelect = false;

                if (m_frmQuery.Visible == false)
                {
                    m_frmQuery.Show((m_Application as IFramework).MdiForm);
                }
                if (m_frmQuery.SelectGeometry != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(m_frmQuery.SelectGeometry);
                    m_frmQuery.SelectGeometry = null;
                }
                m_frmQuery.SelectGeometry = pPolygon;
                m_frmQuery.RefreshTreeView();
            }
        }

        #region ICommand Members

        /// <summary>
        /// 
        /// </summary>
        int ICommand.Bitmap
        {
            get { return 0; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Caption
        {
            get { return "QueryByGeometry"; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Category
        {
            get { return "查询要素"; }
        }
        /// <summary>
        /// 
        /// </summary>
        bool ICommand.Checked
        {
            get { return false; }
        }
        /// <summary>
        /// 
        /// </summary>
        bool ICommand.Enabled
        {
            get
            {
                if (m_Application == null)
                    return false;
                return (m_Application as IFramework).IsSceneCtlActive == false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        int ICommand.HelpContextID
        {
            get { return 0; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.HelpFile
        {
            get { return string.Empty; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Message
        {
            get { return "查询要素"; }
        }
        /// <summary>
        /// 
        /// </summary>
        string ICommand.Name
        {
            get { return "查询要素"; }
        }
        /// <summary>
        /// 
        /// </summary>
        void ICommand.OnClick()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hook"></param>
        void ICommand.OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
                Hook = hook;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        string ICommand.Tooltip
        {
            get
            {
                return "绘制范围查询要素";
            }
        }
        #endregion
    }
}
